package com.it.product.comtroller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.*;
import com.it.product.service.IProductService;
import com.it.product.service.IProductUnitService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;


import com.spzx.product.api.domain.vo.SkuPrice;
import com.spzx.product.api.domain.vo.SkuQuery;
import com.spzx.product.api.domain.vo.SkuStockVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * projectName: spzx-parent
 *
 * @author: 王宝贵
 * time: 2024/9/22 21:27 周日
 * description:
 */
@Tag(name = "商品管理")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductUnitService productUnitService;

    /**
     * 分页查询商品管理
     *
     * @param product
     * @return
     */
    @Operation(summary = "分页列表")
    @GetMapping("/list")
    private TableDataInfo list(Product product) {
        startPage();
        List<Product> productList = productService.selectPage(product);
        return getDataTable(productList);
    }

    //新增商品--需要一些前提条件

    /**
     * 1.
     * 加载商品数据
     *
     * @param id 分类的id
     * @return 品牌的名字
     */
    @Operation(summary = "根据商品分类的id查询分类品牌的名字")
    @GetMapping("brandList/{categoryId}")
    private AjaxResult GetBrandNameById(@PathVariable("categoryId") Long id) {
        List<Brand> brandList = productService.GetBrandNameById(id);
        return success(brandList);
    }

    /**
     * 2.
     * 获取商品单位的名称
     *
     * @return
     */
    @Operation(summary = "获取全部单位")
    @GetMapping("getUnitAll")
    public AjaxResult selectProductUnitAll() {
        return success(productUnitService.list());
    }

    /**
     * 3.
     * 根据分类id查询商品规格的名字
     *
     * @param categoryId
     * @return
     */
    @Operation(summary = "根据分类id获取商品规格列表")
    @GetMapping("/productSpecList/{categoryId}")
    private AjaxResult selectSpecNameById(@PathVariable Long categoryId) {
        return success(productService.selectSpecNameById(categoryId));
    }

    /**
     * 新增商品
     *
     * @param product 四张表的数据
     * @return
     */
    @Operation(summary = "新增商品")
    @PostMapping
    private AjaxResult save(@RequestBody Product product) {
        return toAjax(productService.saveList(product));
    }


    //修改操作前的根据商品id查询信息

    /**
     * 根据商品的id 查询商品的信息
     *
     * @param id 商品的id
     * @return
     */
    @Operation(summary = "获取商品详情信息")
    @GetMapping("/{id}")
    private AjaxResult selectByProductId(@PathVariable Long id) {
        Product product = productService.selectByproductId(id);
        return success(product);
    }

    /**
     * 修改商品
     *
     * @param product
     * @return
     */
    @Operation(summary = "修改商品")
    @PutMapping
    private AjaxResult update(@RequestBody Product product) {
        int row = productService.updateProduct(product);
        return toAjax(row);
    }

    /**
     * 根据商品id删除商品
     *
     * @param ids 商品id
     * @return
     */
    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    private AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productService.removeProductByIds(ids));
    }

    /**
     * 根据商品的id修改商品的状态
     *
     * @param id          商品的id
     * @param auditStatus 商品的状态
     * @return
     */
    @Operation(summary = "审批通过")
    @GetMapping("updateAuditStatus/{id}/{auditStatus}")
    private AjaxResult updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        return toAjax(productService.updateAuditStatus(id, auditStatus));
    }

    /**
     * 更新商品的上下架状态
     *
     * @param id     商品的id
     * @param status 商品的状态 1 上架 -1下架
     * @return
     */
    @Operation(summary = "更新下架")
    @GetMapping("updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return toAjax(productService.updateStatus(id, status));
    }


// =================================以下接口是远程调用的接口=================================

    /**
     * 查询销量前20的商品的信息
     *
     * @return 返回的是以productSku为主表
     */
    @InnerAuth
    @GetMapping("/getTopSale")
    public R<List<ProductSku>> getTopSale() {
        List<ProductSku> productSkuList = productService.getTopSale();
        return R.ok(productSkuList);
    }


    /**
     * 分页条件查询商品信息
     *
     * @param pageNum  当前页
     * @param pageSize 每页记录数
     * @param skuQuery 查询对象
     * @return 返回多个对象
     */
    @InnerAuth
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public R<TableDataInfo> skuList(
            //swagger 注解 name   description描述   required必须传输
            @Parameter(name = "pageNum", description = "当前页码", required = true)
            @PathVariable(name = "pageNum") Integer pageNum,
            @Parameter(name = "pageSize", description = "每页记录数", required = true)
            @PathVariable(name = "pageSize") Integer pageSize,
            @Parameter(name = "skuQuery", description = "查询对象", required = true)
            /**
             * SpringQueryMap将 HTTP 请求的主体（body）映射为 Java 对象  和@RequestBoby相似
             * 一个用于Get请求 一个用于Post或者Put请求
             */
            @SpringQueryMap SkuQuery skuQuery
    ) {
        //自动开启分页 用于简单 复杂用 new IPage
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSku> productSkuList = productService.skuList(skuQuery);
        return R.ok(getDataTable(productSkuList));
    }


    //查询商品详情 --多表查询
    //1.查询商品 product信息 productId
    @InnerAuth
    @GetMapping("/getProduct/{id}")
    public R<Product> getProduct(@PathVariable Long id) {
        Product product = productService.getProduct(id);
        return R.ok(product);
    }

    //2.查询productSku信息 skuId
    @InnerAuth
    @GetMapping("/getProductSku/{skuId}")
    public R<ProductSku> getProductSku(@PathVariable Long skuId) {
        ProductSku ProductSku = productService.getProductSku(skuId);
        return R.ok(ProductSku);
    }

    //3.查询sku价格表 skuId
    @InnerAuth
    @GetMapping("/getSkuPrice/{skuId}")
    public R<SkuPrice> getSkuPrice(@PathVariable Long skuId) {
        SkuPrice SkuPrice = productService.getSkuPrice(skuId);
        return R.ok(SkuPrice);
    }

    //4.查询product_details表的url图片地址
    @InnerAuth
    @GetMapping("/getProductDetails/{id}")
    public R<ProductDetails> getProductDetails(@PathVariable Long id) {
        ProductDetails ProductDetails = productService.getProductDetails(id);
        return R.ok(ProductDetails);
    }

    //5.skuStockVo信息
    @InnerAuth
    @GetMapping("/getSkuStock/{skuId}")
    public R<SkuStockVo> getSkuStock(@PathVariable Long skuId) {
        SkuStockVo SkuStockVo = productService.getSkuStock(skuId);
        return R.ok(SkuStockVo);
    }

    //6.skuSpecValueMap信息skuId
    //以map集合来接收 key为sku表的sku_spc  值为sku的主键id
    @InnerAuth
    @GetMapping("/getSkuSpecValue/{id}")
    public R<Map<String,Long>> getSkuSpecValue(@PathVariable Long id){
        Map<String,Long> map = productService.getSkuSpecValue(id);
        return R.ok(map);
    }

    //批量查询购物车的价格 之前6个接口之一的单个skuId--远程
    @Operation(summary = "批量获取商品sku最新价格信息")
    @InnerAuth
    @PostMapping(value = "/getSkuPriceList")
    public R<List<SkuPrice>> getSkuPriceList(@RequestBody List<Long> skuIdList){
        List<SkuPrice> skuPrices = productService.getSkuPriceList(skuIdList);
        return R.ok(skuPrices);
    }

    @InnerAuth
    @Operation(summary = "检查与锁定库存")
    @PostMapping("checkAndLock/{orderNo}")
    public R<String> checkAndLock(@PathVariable String orderNo, @RequestBody List<SkuLockVo> skuLockVoList) {
        try {
            return R.ok(productService.checkAndLock(orderNo,skuLockVoList));
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.ok(e.getMessage());
        }catch (Exception e1){
            e1.printStackTrace();
            return R.ok("库存不足");
        }
    }



}